home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_068 / mg1b / sys / amiga / fileio.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  202 lines

  1. /*
  2.  * Name:    MicroEMACS
  3.  * Version:    Gnu v30
  4.  *        Commodore Amiga file I/O.
  5.  * Last edit:    13-Dec-86 ...ihnp4!seismo!ut-sally!ut-ngp!mic
  6.  * Created:    23-Jul-86 ...ihnp4!seismo!ut-sally!ut-ngp!mic
  7.  *        (from sys/bsd/fileio.c)
  8.  *
  9.  * Read and write ASCII files. All
  10.  * of the low level file I/O knowledge is here.
  11.  * Pretty much vanilla standard I/O, except for
  12.  * fbackupfile()
  13.  */
  14. #include    <libraries/dos.h>
  15. #include    <libraries/dosextens.h>
  16.  
  17. #undef    TRUE
  18. #undef    FALSE
  19. #include    "def.h"
  20. /* With Lattice, make sure you use -Idf0:include/lattice/ to    */
  21. /* put the Lattice files in the right spot.            */
  22.  
  23. static    FILE    *ffp;
  24.  
  25. /*
  26.  * Open a file for reading.
  27.  */
  28. ffropen(fn)
  29. char    *fn;
  30. {
  31.     if ((ffp=fopen(fn, "r")) == NULL)
  32.         return (FIOFNF);
  33.     return (FIOSUC);
  34. }
  35.  
  36. /*
  37.  * Open a file for writing.
  38.  * Return TRUE if all is well, and
  39.  * FALSE on error (cannot create).
  40.  */
  41. ffwopen(fn)
  42. char    *fn;
  43. {
  44.     if ((ffp=fopen(fn, "w")) == NULL) {
  45.         ewprintf("Cannot open file for writing");
  46.         return (FIOERR);
  47.     }
  48.     return (FIOSUC);
  49. }
  50.  
  51. /*
  52.  * Close a file.
  53.  * Should look at the status.
  54.  */
  55. ffclose()
  56. {
  57.     fclose(ffp);
  58.     return (FIOSUC);
  59. }
  60.  
  61. /*
  62.  * Write a line to the already
  63.  * opened file. The "buf" points to the
  64.  * buffer, and the "nbuf" is its length, less
  65.  * the free newline. Return the status.
  66.  * Check only at the newline.
  67.  */
  68. ffputline(buf, nbuf)
  69. register char    buf[];
  70. {
  71.     register int    i;
  72.  
  73.     for (i=0; i<nbuf; ++i)
  74.         putc(buf[i]&0xFF, ffp);
  75.     putc('\n', ffp);
  76.     if (ferror(ffp) != FALSE) {
  77.         ewprintf("Write I/O error");
  78.         return (FIOERR);
  79.     }
  80.     return (FIOSUC);
  81. }
  82.  
  83. /*
  84.  * Read a line from a file, and store the bytes
  85.  * in the supplied buffer. Stop on end of file or end of
  86.  * line. Don't get upset by files that don't have an end of
  87.  * line on the last line; this seem to be common on CP/M-86 and
  88.  * MS-DOS (the suspected culprit is VAX/VMS kermit, but this
  89.  * has not been confirmed. If this is sufficiently researched
  90.  * it may be possible to pull this kludge). Delete any CR
  91.  * followed by an LF.
  92.  */
  93. ffgetline(buf, nbuf)
  94. register char    buf[];
  95. {
  96.     register int    c;
  97.     register int    i;
  98.  
  99.     i = 0;
  100.     for (;;) {
  101.         c = getc(ffp);
  102.         if (c == '\r') {        /* Delete any non-stray    */
  103.             c = getc(ffp);        /* carriage returns.    */
  104.             if (c != '\n') {
  105.                 if (i >= nbuf-1) {
  106.                     ewprintf("File has long line");
  107.                     return (FIOERR);
  108.                 }
  109.                 buf[i++] = '\r';
  110.             }
  111.         }
  112.         if (c==EOF || c=='\n')        /* End of line.        */
  113.             break;
  114.         if (i >= nbuf-1) {
  115.             ewprintf("File has long line");
  116.             return (FIOERR);
  117.         }
  118.         buf[i++] = c;
  119.     }
  120.     if (c == EOF) {                /* End of file.        */
  121.         if (ferror(ffp) != FALSE) {
  122.             ewprintf("File read error");
  123.             return (FIOERR);
  124.         }
  125.         if (i == 0)            /* Don't get upset if    */
  126.             return (FIOEOF);    /* no newline at EOF.    */
  127.     }
  128.     buf[i] = 0;
  129.     return (FIOSUC);
  130. }
  131.  
  132. #ifdef    BACKUP
  133. /*
  134.  * Rename the current file into a backup copy,
  135.  * possibly after deleting the original file.
  136.  */
  137. fbackupfile(fname)
  138. char    *fname;
  139. {
  140.     struct FileLock *twiddle, *lock, *Lock();
  141.     ULONG Rename(), UnLock();
  142.     char buffer[NFILEN];
  143.  
  144.     (void) strncpy(buffer,fname,NFILEN - 1);
  145.     (void) strcat(buffer,"~");
  146.  
  147.     lock = Lock(fname,(ULONG)EXCLUSIVE_LOCK);/* does file exist?    */
  148.     if (!lock)
  149.         return (FALSE);            /* nope, return error    */
  150.  
  151.     twiddle = Lock(buffer,(ULONG)EXCLUSIVE_LOCK);
  152.     if (twiddle) {                /* delete old backup    */
  153.         UnLock(twiddle);        /* let it go        */
  154.         if (!DeleteFile(buffer)) {
  155.             UnLock(lock);
  156.             return (FALSE);
  157.         }
  158.         twiddle = NULL;
  159.     }
  160.     /* rename file to backup name (after unlocking the file)
  161.      */
  162.     UnLock(lock);
  163.     return (int) Rename(fname,buffer);
  164. }
  165. #endif    BACKUP
  166.  
  167. #ifdef    STARTUP
  168. /*
  169.  * Return name of user's startup file.  On Amiga, make it
  170.  * s:.mg
  171.  */
  172.  
  173. static char startname[] = ".mg";
  174. static char altstartname[] = "s:.mg";
  175.  
  176. char *startupfile()
  177. {
  178.     FILE *f, *fopen();
  179.     if (f = fopen(startname,"r")) {        /* first try    */
  180.         fclose(f);
  181.         return(startname);
  182.     }
  183.     if (f = fopen(altstartname,"r")) {    /* second try    */
  184.         fclose(f);
  185.         return (altstartname);
  186.     }
  187.     return (NULL);
  188. }
  189. #endif    STARTUP
  190.  
  191. /*
  192.  * The string "fn" is a file name.
  193.  * Perform any required case adjustments.
  194.  * On the Amiga file names are dual case,
  195.  * so we leave everything alone.
  196.  */
  197. adjustcase(fn)
  198. register char    *fn;
  199. {
  200.     return (TRUE);
  201. }
  202.